
#include "bsp_driver_uart.h"


/****************************************************                        
*				stm32f103x_bsp_init_uart1
* Description:	
* Arguments  :  
* Returns    :  
*****************************************************/
void stm32f103x_bsp_init_Uart1( uint32_t u_BaudRate,
							    uint16_t u_DataBit,
							    uint16_t u_StopBit,
								uint16_t u_parity ){

    USART_InitTypeDef       usart_init;
    USART_ClockInitTypeDef  usart_clk_init;


	RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1| 
							RCC_APB2Periph_AFIO, ENABLE );

	//			INIT USART STRUCT
	usart_init.USART_BaudRate            = u_BaudRate;
    usart_init.USART_WordLength          = u_DataBit;
    usart_init.USART_StopBits            = u_StopBit;
    usart_init.USART_Parity              = u_parity ;
    usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    usart_init.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(  USART1, &usart_init );
	
    usart_clk_init.USART_Clock           = USART_Clock_Disable;
    usart_clk_init.USART_CPOL            = USART_CPOL_Low;
    usart_clk_init.USART_CPHA            = USART_CPHA_2Edge;
    usart_clk_init.USART_LastBit         = USART_LastBit_Disable;
	USART_ClockInit( USART1, &usart_clk_init );

	//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	
    USART_Cmd( USART1, ENABLE );
}


/****************************************************                        
*				stm32f103x_bsp_send_Uart1
* Description:	
* Arguments  :  
* Returns    :  
*****************************************************/
uint16_t stm32f103x_bsp_send_Uart1( unsigned char *p_Data, 
								    uint16_t u_Size ){


	uint16_t i = 0;


	for(i=0;i<u_Size;i++){
		
		while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);	 

		USART_SendData(USART1, p_Data[i]);	
	}

	return u_Size;
}

/****************************************************                        
*				stm32f103x_bsp_init_uart1
* Description:	
* Arguments  :  
* Returns    :  
*****************************************************/
void stm32f103x_bsp_init_Uart3( uint32_t u_BaudRate,
							    uint16_t u_DataBit,
							    uint16_t u_StopBit,
								uint16_t u_parity ){

    USART_InitTypeDef       usart_init;
    USART_ClockInitTypeDef  usart_clk_init;

    
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3,ENABLE );

	RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE );

	//			INIT USART STRUCT
	usart_init.USART_BaudRate            = u_BaudRate;
    usart_init.USART_WordLength          = u_DataBit;
    usart_init.USART_StopBits            = u_StopBit;
    usart_init.USART_Parity              = u_parity ;
    usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    usart_init.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(  USART3, &usart_init );
	
    usart_clk_init.USART_Clock           = USART_Clock_Disable;
    usart_clk_init.USART_CPOL            = USART_CPOL_Low;
    usart_clk_init.USART_CPHA            = USART_CPHA_2Edge;
    usart_clk_init.USART_LastBit         = USART_LastBit_Disable;
	USART_ClockInit( USART3, &usart_clk_init );

	//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	
    USART_Cmd( USART3, ENABLE );
}


/****************************************************                        
*				stm32f103x_bsp_send_Uart1
* Description:	
* Arguments  :  
* Returns    :  
*****************************************************/
uint16_t stm32f103x_bsp_send_Uart3( unsigned char *p_Data, 
								    uint16_t u_Size ){


	uint16_t i = 0;


	for(i=0;i<u_Size;i++){
		
		while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);	 

		USART_SendData(USART3, p_Data[i]);	
	}

	return u_Size;
}
